
//待打印列表

package print

import (
	"time"
	"dhfshop/models"
	"dhfshop/models/consts"
	//"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

// 打印队列
type PrintQueue struct {
	FlowId       int64     `orm:"column(flow_id);pk;auto" json:"flow_id" form:"flow_id"`           //消息流水ID
	PartnerId    int64     `orm:"column(partner_id)" json:"partner_id" form:"partner_id"`          //组织编号
	StoreId      int64     `orm:"column(store_id)" json:"store_id" form:"store_id"`                //门店编号
	PosId        string    `orm:"column(pos_id)" json:"pos_id" form:"pos_id"`                      //终端号POS_ID
	DataType     int       `orm:"column(data_type)" json:"data_type" form:"data_type"`             //数据类型
	PrintContent string    `orm:"column(print_content)" json:"print_content" form:"print_content"` //打印内容
	PrintNum     int       `orm:"column(print_num)" json:"print_num" form:"print_num"`             //当次打印次数
	PrintCount   int       `orm:"column(print_count)" json:"print_count" form:"print_count"`       //已打印次数
	IsRead       int       `orm:"column(is_read);default(0)" json:"is_read" form:"is_read"`        //是否读取(0未读取1已读取)
	IsPrint      int       `orm:"column(is_print);default(0)" json:"is_print" form:"is_print"`     //是否打印(0未打印1打印成功2打印失败3打印异常)
	CreatedAt    time.Time `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"`
	CreatedAtStr string    `orm:"-" json:"created_at"`
	UpdatedAt    time.Time `orm:"column(updated_at);auto_now;type(datetime)" json:"-"`
	UpdatedAtStr string    `orm:"-" json:"updated_at"`
}

func init() {
	orm.RegisterModel(new(PrintQueue))
}

func AddPrintQueue(printQueue *PrintQueue) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(printQueue)
	return
}

func GetPrintQueuesByStoreId(storeId int64) (msgs []*PrintQueue, err error) {
	msgs = []*PrintQueue{}
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(PrintQueue)).Filter("store_id",storeId).Filter("is_read",0)
	_, err = qs.All(&msgs)
	for _, item := range msgs {
		item.CreatedAtStr = item.CreatedAt.Format(consts.DATETIME_FORMAT)
	}
	return
}

func GetPrintQueuesByPosId(posId int64) (msgs []*PrintQueue, err error) {
	msgs = []*PrintQueue{}
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(PrintQueue)).Filter("pos_id", posId).Filter("is_read",0)
	_, err = qs.All(&msgs)
	for _, item := range msgs {
		item.CreatedAtStr = item.CreatedAt.Format(consts.DATETIME_FORMAT)
	}
	return
}

func GetAllPrintQueues(partnerId int64,start,end string,isRead int,OrderByDesc,offset,limit int)([]*PrintQueue,int64,error) {
	fields := []string{"flow_id","pos_id","print_content","is_read","created_at"}
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(PrintQueue))
	if len(start) == 19 {
	    if startTime, err := time.Parse(consts.DATETIME_FORMAT, start); err == nil {
		    qs = qs.Filter("created_at__gte", startTime)
		}
	}
	if len(end) == 19 {
	    if endTime, err := time.Parse(consts.DATETIME_FORMAT, end); err == nil {
		    qs = qs.Filter("created_at__lte", endTime)
		}
	}
	if isRead == 1 {
	    qs = qs.Filter("is_read", 0)   //未读取
	}
	if isRead == 2 {
	    qs = qs.Filter("is_read", 1)   //已读取
	}
	cnt, err := qs.Count()
	if OrderByDesc == 0 {
	   qs = qs.OrderBy("-created_at")
	}else{
	   qs = qs.OrderBy("created_at")
	}
	var lists []*PrintQueue
	if _, err = qs.Limit(limit, offset).All(&lists, fields...); err == nil {
		for _, item := range lists {
			item.CreatedAtStr = item.CreatedAt.Format(consts.DATETIME_FORMAT)
	    }
		return lists, cnt, nil
	}
	return nil, 0, err
}

func UpdatePrintQueueById(m *PrintQueue, fields ...string) error {
	o := models.GetDefaultDB()
	v := PrintQueue{FlowId: m.FlowId}
	if err := o.Read(&v); err != nil {
		return err
	}
	_, err := o.Update(m, fields...)
	return err
}

func DeletePrintQueueById(flowId int64) (err error) {
	o := models.GetDefaultDB()
	v := PrintQueue{FlowId: flowId}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return nil
		}
	}
	return
}
